package com.tomtop.example_redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import com.tomtop.core.redis.JsonBytesRedisTemplate;
import com.tomtop.core.utils.JSONUtils;

@Component
public class RedisTest implements CommandLineRunner {
	@Autowired RedisTemplate<String, User> redisTemplate;
	@Autowired JsonBytesRedisTemplate jsonBytesRedisTemplate;
	@Override
	public void run(String... args) throws Exception {
		String cachekey="user-cachekey-22222";
		redisTemplate.opsForValue().set(cachekey, new User(33333, "test-user-33333"));
		User user = redisTemplate.opsForValue().get(cachekey);
		System.err.println(JSONUtils.toJSONString(user));
		BoundValueOperations<String, Object> jsonuser = jsonBytesRedisTemplate.boundValueOps(cachekey);
		jsonuser.set(user);
		System.err.println(new String((byte[])jsonuser.get()));
		user=JSONUtils.fromObject((byte[])jsonuser.get(), User.class);
		System.err.println(JSONUtils.toJSONString(user));
	}
}
